#include "touch_calibration_nine.h"
#include <unistd.h>
#include <QPainter>
#include <QFile>
#include <QTimer>
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QScreen>
#include <QTextStream>
//zhaolunhan 2019/6/3
#include <QDebug>
//#include "../../../../include/components/linguist/linguist_const_define.h"
//#include "../../../../include/components/gui/gui_idp.h"
//#include "../../../../include/components/log/logger_idp.h"

//zhaolunhan 2019/6/3
/*const QEvent::Type calibration_notify_event::_calibration_notify_event =
    static_cast<QEvent::Type>(QEvent::registerEventType(mcgs_gui_user_customized_event_identifier::MCGS_GUI_CALIBRATION_NOTIFY_EVENT_ID + QEvent::User));*/

calibration_notify_event::calibration_notify_event(Type e)
: QEvent(e)
{}

/**********************************************************************************************/

touch_calibration_nine::touch_calibration_nine(QWidget* parent)
    : /*modal_base_dialog(parent, Qt::FramelessWindowHint),*/ //zhaolunhan 2019/6/3
  _pressCount(0), _algorithm_points(0), _press_timer(NULL),
  _draw_timer(NULL), _monitor_client(NULL),
  _which(-1), _average_index(0), _total_num(0),
  _calibration_ret(true)
{
    set_ui(this);

    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect screenRect = desktopWidget->screenGeometry();
    setFixedSize(QSize(screenRect.width(), screenRect.height()));

    setFocusPolicy(Qt::StrongFocus);
    setFocus();
    setModal(true);

    double width = qt_screen->deviceWidth();
    double height = qt_screen->deviceHeight();

    double dx = width / 16;
    double dy = height / 16;

    if(qt_screen->deviceWidth() > 1024)
    {
        dx = width / 20;
        dy = height / 18;
    }

    QPoint* points = _data.screenPoints;
    QPoint* former_points = _former_data.screenPoints;

    if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
    {
        _algorithm_points = 9;

        double offset_dy = (height / 2 - dy) / MCGS_CALIBRATION_SCREEN_POINTS_NINE;
        double offset_dx = (width / 2  - dx) / MCGS_CALIBRATION_SCREEN_POINTS_NINE;

        if(qt_screen->transformOrientation() == 0)
        {
            //never rotation
            _screen_nine_matrix[SCREEN_CENTRE] = QPoint(width / 2, height / 2);
            _screen_nine_matrix[SCREEN_TOP] = QPoint(width / 2, dy);
            _screen_nine_matrix[SCREEN_TOP_LEFT] = QPoint(dx, dy);
            _screen_nine_matrix[SCREEN_LEFT] = QPoint(dx, height / 2);
            _screen_nine_matrix[SCREEN_BOTTOM_LEFT] = QPoint(dx, height - dy);
            _screen_nine_matrix[SCREEN_BOTTOM] = QPoint(width / 2, height - dy);
            _screen_nine_matrix[SCREEN_BOTTOM_RIGHT] = QPoint(width - dx, height - dy);
            _screen_nine_matrix[SCREEN_RIGHT] = QPoint(width - dx, height / 2);
            _screen_nine_matrix[SCREEN_TOP_RIGHT] = QPoint(width - dx, dy);
        }
        else if(qt_screen->transformOrientation() == 1)
        {
            //clockwise rotation 90 degrees
            _screen_nine_matrix[SCREEN_CENTRE] = QPoint(width / 2, height / 2);
            _screen_nine_matrix[SCREEN_TOP] = QPoint(dx, height / 2);
            _screen_nine_matrix[SCREEN_TOP_LEFT] = QPoint(dx, height - dy);
            _screen_nine_matrix[SCREEN_LEFT] = QPoint(width / 2, height - dy);
            _screen_nine_matrix[SCREEN_BOTTOM_LEFT] = QPoint(width - dx, height - dy);
            _screen_nine_matrix[SCREEN_BOTTOM] = QPoint(width - dx, height / 2);
            _screen_nine_matrix[SCREEN_BOTTOM_RIGHT] = QPoint(width - dx, dy);
            _screen_nine_matrix[SCREEN_RIGHT] = QPoint(width / 2, dy);
            _screen_nine_matrix[SCREEN_TOP_RIGHT] = QPoint(dx, dy);
        }
        else if(qt_screen->transformOrientation() == 2)
        {
            //clockwise rotation 180 degrees
            _screen_nine_matrix[SCREEN_CENTRE] = QPoint(width / 2, height / 2);
            _screen_nine_matrix[SCREEN_TOP] = QPoint(width / 2, height - dy);
            _screen_nine_matrix[SCREEN_TOP_LEFT] = QPoint(width - dx, height - dy);
            _screen_nine_matrix[SCREEN_LEFT] = QPoint(width - dx, height / 2);
            _screen_nine_matrix[SCREEN_BOTTOM_LEFT] = QPoint(width - dx, dy);
            _screen_nine_matrix[SCREEN_BOTTOM] = QPoint(width / 2, dy);
            _screen_nine_matrix[SCREEN_BOTTOM_RIGHT] = QPoint(dx, dy);
            _screen_nine_matrix[SCREEN_RIGHT] = QPoint(dx, height / 2);
            _screen_nine_matrix[SCREEN_TOP_RIGHT] = QPoint(dx, height - dy);
        }
        else if(qt_screen->transformOrientation() == 3)
        {
            //clockwise rotation 270 degrees
            _screen_nine_matrix[SCREEN_CENTRE] = QPoint(width / 2, height / 2);
            _screen_nine_matrix[SCREEN_TOP] = QPoint(width - dx, height / 2);
            _screen_nine_matrix[SCREEN_TOP_LEFT] = QPoint(width - dx, dy);
            _screen_nine_matrix[SCREEN_LEFT] = QPoint(width / 2, dy);
            _screen_nine_matrix[SCREEN_BOTTOM_LEFT] = QPoint(dx, dy);
            _screen_nine_matrix[SCREEN_BOTTOM] = QPoint(dx, height / 2);
            _screen_nine_matrix[SCREEN_BOTTOM_RIGHT] = QPoint(dx, height - dy);
            _screen_nine_matrix[SCREEN_RIGHT] = QPoint(width / 2, height - dy);
            _screen_nine_matrix[SCREEN_TOP_RIGHT] = QPoint(width - dx, height - dy);
        }
        else
        {}

        points[QWSPointerCalibrationData::TopLeft] = _screen_nine_matrix[SCREEN_TOP_LEFT];
        points[QWSPointerCalibrationData::BottomRight] = _screen_nine_matrix[SCREEN_BOTTOM_RIGHT];
        points[QWSPointerCalibrationData::TopRight] = _screen_nine_matrix[SCREEN_TOP_RIGHT];

        //never rotation
        if(qt_screen->transformOrientation() == 0)
        {
            former_points[QWSPointerCalibrationData::TopLeft] = _screen_nine_matrix[SCREEN_TOP_LEFT];
            former_points[QWSPointerCalibrationData::BottomLeft] = _screen_nine_matrix[SCREEN_BOTTOM_LEFT];
            former_points[QWSPointerCalibrationData::BottomRight] =  _screen_nine_matrix[SCREEN_BOTTOM_RIGHT];
            former_points[QWSPointerCalibrationData::TopRight] = _screen_nine_matrix[SCREEN_TOP_RIGHT];
            former_points[QWSPointerCalibrationData::Center] = _screen_nine_matrix[SCREEN_CENTRE];

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points_for_nine[SCREEN_CENTRE][i] = QPoint(width / 2, dy + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_TOP][i] = QPoint(dx + n * offset_dx, dy);
                _other_screen_points_for_nine[SCREEN_TOP_LEFT][i] = QPoint(dx, height / 2 - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_LEFT][i] = QPoint(dx, height - dy - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_LEFT][i] = QPoint(width / 2 - n * offset_dx, height - dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM][i] = QPoint(width - dx - n * offset_dx, height - dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_RIGHT][i] = QPoint(width - dx, height / 2 + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_RIGHT][i] = QPoint(width - dx, dy + n * offset_dy);
            }
        }

        //clockwise rotation 90 degrees
        if(qt_screen->transformOrientation() == 1)
        {
            former_points[QWSPointerCalibrationData::TopLeft] = _screen_nine_matrix[SCREEN_BOTTOM_LEFT];
            former_points[QWSPointerCalibrationData::BottomLeft] = _screen_nine_matrix[SCREEN_BOTTOM_RIGHT];
            former_points[QWSPointerCalibrationData::BottomRight] = _screen_nine_matrix[SCREEN_TOP_RIGHT];
            former_points[QWSPointerCalibrationData::TopRight] = _screen_nine_matrix[SCREEN_TOP_LEFT];
            former_points[QWSPointerCalibrationData::Center] = _screen_nine_matrix[SCREEN_CENTRE];

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points_for_nine[SCREEN_CENTRE][i] = QPoint(dx + n * offset_dx, height / 2);
                _other_screen_points_for_nine[SCREEN_TOP][i] = QPoint(dx, height - dy - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_TOP_LEFT][i] = QPoint(width / 2 - n * offset_dx, height - dy);
                _other_screen_points_for_nine[SCREEN_LEFT][i] = QPoint(width - dx - n * offset_dx, height - dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_LEFT][i] = QPoint(width - dx, height / 2 + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM][i] = QPoint(width - dx, dy + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_RIGHT][i] = QPoint(width / 2 + n * offset_dx, dy);
                _other_screen_points_for_nine[SCREEN_RIGHT][i] = QPoint(dx + n * offset_dx, dy);
            }
        }

        //clockwise rotation 180 degrees
        if(qt_screen->transformOrientation() == 2)
        {
            former_points[QWSPointerCalibrationData::TopLeft] = _screen_nine_matrix[SCREEN_BOTTOM_RIGHT];
            former_points[QWSPointerCalibrationData::BottomLeft] = _screen_nine_matrix[SCREEN_TOP_RIGHT];
            former_points[QWSPointerCalibrationData::BottomRight] = _screen_nine_matrix[SCREEN_TOP_LEFT];
            former_points[QWSPointerCalibrationData::TopRight] = _screen_nine_matrix[SCREEN_BOTTOM_LEFT];
            former_points[QWSPointerCalibrationData::Center] = _screen_nine_matrix[SCREEN_CENTRE];

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points_for_nine[SCREEN_CENTRE][i] = QPoint(width / 2, height - dy - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_TOP][i] = QPoint(width - dx - n * offset_dx, height - dy);
                _other_screen_points_for_nine[SCREEN_TOP_LEFT][i] = QPoint(width - dx, height / 2 + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_LEFT][i] = QPoint(width - dx, dy + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_LEFT][i] = QPoint(width / 2 + n * offset_dx, dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM][i] = QPoint(dx + n * offset_dx, dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_RIGHT][i] = QPoint(dx, height / 2 - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_RIGHT][i] = QPoint(dx, height - dy - n * offset_dy);
            }
        }

        //clockwise rotation 270 degrees
        if(qt_screen->transformOrientation() == 3)
        {
            former_points[QWSPointerCalibrationData::TopLeft] = _screen_nine_matrix[SCREEN_TOP_RIGHT];
            former_points[QWSPointerCalibrationData::BottomLeft] = _screen_nine_matrix[SCREEN_TOP_LEFT];
            former_points[QWSPointerCalibrationData::BottomRight] = _screen_nine_matrix[SCREEN_BOTTOM_LEFT];
            former_points[QWSPointerCalibrationData::TopRight] = _screen_nine_matrix[SCREEN_BOTTOM_RIGHT];
            former_points[QWSPointerCalibrationData::Center] = _screen_nine_matrix[SCREEN_CENTRE];

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points_for_nine[SCREEN_CENTRE][i] = QPoint(width - dx - n * offset_dx , height / 2);
                _other_screen_points_for_nine[SCREEN_TOP][i] = QPoint(width - dx, dy + n * offset_dy);
                _other_screen_points_for_nine[SCREEN_TOP_LEFT][i] = QPoint(width / 2 + n * offset_dx, dy);
                _other_screen_points_for_nine[SCREEN_LEFT][i] = QPoint(dx + n * offset_dx, dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_LEFT][i] = QPoint(dx, height / 2 - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM][i] = QPoint(dx, height - dy - n * offset_dy);
                _other_screen_points_for_nine[SCREEN_BOTTOM_RIGHT][i] = QPoint(width / 2 - n * offset_dx, height - dy);
                _other_screen_points_for_nine[SCREEN_RIGHT][i] = QPoint(width - dx - n * offset_dx, height - dy);
            }
        }
    }
    else
    {
        _algorithm_points = 5;

        double t_b_dy = (height - dy - dy) / MCGS_CALIBRATION_SCREEN_POINTS_NINE;
        double t_b_dx = (width - dx - dx) / MCGS_CALIBRATION_SCREEN_POINTS_NINE;
        double tr_ct_dx = (width / 2 - dx) / MCGS_CALIBRATION_SCREEN_POINTS_NINE;
        double tr_ct_dy = (height / 2 - dy) / MCGS_CALIBRATION_SCREEN_POINTS_NINE;

        //never rotation
        if(qt_screen->transformOrientation() == 0)
        {
            points[QWSPointerCalibrationData::TopLeft] = QPoint(dx, dy);
            points[QWSPointerCalibrationData::BottomLeft] = QPoint(dx, height - dy);
            points[QWSPointerCalibrationData::BottomRight] = QPoint(width - dx, height - dy);
            points[QWSPointerCalibrationData::TopRight] = QPoint(width - dx, dy);
            points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            former_points[QWSPointerCalibrationData::TopLeft] = QPoint(dx, dy);
            former_points[QWSPointerCalibrationData::BottomLeft] = QPoint(dx, height - dy);
            former_points[QWSPointerCalibrationData::BottomRight] = QPoint(width - dx, height - dy);
            former_points[QWSPointerCalibrationData::TopRight] = QPoint(width - dx, dy);
            former_points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points[QWSPointerCalibrationData::TopLeft][i] = QPoint(dx, height - dy - n * t_b_dy);
                _other_screen_points[QWSPointerCalibrationData::BottomLeft][i] = QPoint(width - dx - n * t_b_dx, height - dy);
                _other_screen_points[QWSPointerCalibrationData::BottomRight][i] = QPoint(width - dx, dy + n * t_b_dy);
                _other_screen_points[QWSPointerCalibrationData::TopRight][i] = QPoint(width / 2 + n * tr_ct_dx, height / 2 - n * tr_ct_dy);
            }
        }

        //clockwise rotation 90 degrees
        if(qt_screen->transformOrientation() == 1)
        {
            points[QWSPointerCalibrationData::TopRight] = QPoint(dx, dy);
            points[QWSPointerCalibrationData::TopLeft] = QPoint(dx, height - dy);
            points[QWSPointerCalibrationData::BottomLeft] = QPoint(width - dx, height - dy);
            points[QWSPointerCalibrationData::BottomRight] = QPoint(width - dx, dy);
            points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            former_points[QWSPointerCalibrationData::TopRight] = QPoint(dx, dy);
            former_points[QWSPointerCalibrationData::TopLeft] = QPoint(dx, height - dy);
            former_points[QWSPointerCalibrationData::BottomLeft] = QPoint(width - dx, height - dy);
            former_points[QWSPointerCalibrationData::BottomRight] = QPoint(width - dx, dy);
            former_points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points[QWSPointerCalibrationData::TopLeft][i] = QPoint(width - dx - n * t_b_dx, height - dy);
                _other_screen_points[QWSPointerCalibrationData::BottomLeft][i] = QPoint(width - dx, dy + n * t_b_dy);
                _other_screen_points[QWSPointerCalibrationData::BottomRight][i] = QPoint(dx + n * t_b_dx, dy);
                _other_screen_points[QWSPointerCalibrationData::TopRight][i] = QPoint(width / 2 - n * tr_ct_dx, height / 2 - n * tr_ct_dy);
            }
        }

        //clockwise rotation 180 degrees
        if(qt_screen->transformOrientation() == 2)
        {
            points[QWSPointerCalibrationData::BottomRight] = QPoint(dx, dy);
            points[QWSPointerCalibrationData::TopRight] = QPoint(dx, height - dy);
            points[QWSPointerCalibrationData::TopLeft] = QPoint(width - dx, height - dy);
            points[QWSPointerCalibrationData::BottomLeft] = QPoint(width - dx, dy);
            points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            former_points[QWSPointerCalibrationData::BottomRight] = QPoint(dx, dy);
            former_points[QWSPointerCalibrationData::TopRight] = QPoint(dx, height - dy);
            former_points[QWSPointerCalibrationData::TopLeft] = QPoint(width - dx, height - dy);
            former_points[QWSPointerCalibrationData::BottomLeft] = QPoint(width - dx, dy);
            former_points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points[QWSPointerCalibrationData::TopLeft][i] = QPoint(width - dx, dy + n * t_b_dy);
                _other_screen_points[QWSPointerCalibrationData::BottomLeft][i] = QPoint(dx + n * t_b_dx, dy);
                _other_screen_points[QWSPointerCalibrationData::BottomRight][i] = QPoint(dx, height- dy - n * t_b_dy);
                _other_screen_points[QWSPointerCalibrationData::TopRight][i] = QPoint(width / 2 - n * tr_ct_dx, height / 2 + n * tr_ct_dy);
            }
        }

        //clockwise rotation 270 degrees
        if(qt_screen->transformOrientation() == 3)
        {
            points[QWSPointerCalibrationData::BottomLeft] = QPoint(dx, dy);
            points[QWSPointerCalibrationData::BottomRight] = QPoint(dx, height - dy);
            points[QWSPointerCalibrationData::TopRight] = QPoint(width - dx, height - dy);
            points[QWSPointerCalibrationData::TopLeft] = QPoint(width - dx, dy);
            points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            former_points[QWSPointerCalibrationData::BottomLeft] = QPoint(dx, dy);
            former_points[QWSPointerCalibrationData::BottomRight] = QPoint(dx, height - dy);
            former_points[QWSPointerCalibrationData::TopRight] = QPoint(width - dx, height - dy);
            former_points[QWSPointerCalibrationData::TopLeft] = QPoint(width - dx, dy);
            former_points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);

            for(int i = 0, n = MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1; i < MCGS_CALIBRATION_SCREEN_POINTS_NINE; ++i, --n)
            {
                _other_screen_points[QWSPointerCalibrationData::TopLeft][i] = QPoint(dx + n * t_b_dx, dy);
                _other_screen_points[QWSPointerCalibrationData::BottomLeft][i] = QPoint(dx, height -dy - n * t_b_dy);
                _other_screen_points[QWSPointerCalibrationData::BottomRight][i] = QPoint(width - dx - n * t_b_dx, height - dy);
                _other_screen_points[QWSPointerCalibrationData::TopRight][i] = QPoint(width / 2 + n * tr_ct_dx, height / 2 + n * tr_ct_dy);
            }
        }
    }

    QWSServer::mouseHandler()->getCalibration(&_former_data);
    //read_calibration_paras();

    _press_timer = new QTimer(this);
    connect(_press_timer, SIGNAL(timeout()), this, SLOT(probe_calibration()));

    _draw_timer = new QTimer(this);
    connect(_draw_timer, SIGNAL(timeout()), this, SLOT(draw_cross()));

    _monitor_client = new QTimer(this);
    connect(_monitor_client, SIGNAL(timeout()), this, SLOT(forbid_window_popped()));
    _monitor_client->start(50);

    memset(_average_array, 0, sizeof(_average_array));
}

touch_calibration_nine::~touch_calibration_nine()
{}

int touch_calibration_nine::calibration_exec()
{
    QWSServer::mouseHandler()->clearCalibration();
    grabMouse();
    activateWindow();
    int ret = exec();
    releaseMouse();

    _monitor_client->stop();
    client_window_show();

    return ret;
}

int touch_calibration_nine::calibration_exec_without_clear()
{
    grabMouse();
    activateWindow();
    int ret = exec();
    releaseMouse();

    _monitor_client->stop();
    client_window_show();

    return ret;
}

void touch_calibration_nine::paintEvent(QPaintEvent* event)
{
    QPainter p(this);

    QPoint point;
    if(_pressCount == 0)
    {
        if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
        {
            point = _screen_nine_matrix[_pressCount];
        }
        else
        {
            point = _data.screenPoints[_pressCount];
        }
    }
    else if(_pressCount >= _algorithm_points)
    {
        calibration_confirm_ui(&p);
        return;
    }
    else
    {
        if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
        {
            point = _other_screen_points_for_nine[_pressCount - 1][_which];
        }
        else
        {
            point = _other_screen_points[_pressCount - 1][_which];
        }
    }

    //Map to logical coordinates in case the screen is transformed
    QSize screenSize(qt_screen->deviceWidth(), qt_screen->deviceHeight());
    point = qt_screen->mapFromDevice(point, screenSize);

    p.fillRect(event->rect(), Qt::white);

    if(qt_screen->deviceWidth() > 1024)
    {
        p.fillRect(point.x() - 30, point.y() - 1,  60, 2,  Qt::black);
        p.fillRect(point.x() - 1,  point.y() - 30, 2,  60, Qt::black);
        p.fillRect(point.x() - 1,  point.y() - 1,  2,  2,  Qt::white);
    }
    else
    {
        p.fillRect(point.x() - 20, point.y() - 1,  40, 2,  Qt::black);
        p.fillRect(point.x() - 1,  point.y() - 20, 2,  40, Qt::black);
        p.fillRect(point.x() - 1,  point.y() - 1,  2,  2,  Qt::white);
    }

    /*5 points algorithm*/
    /*This is test code for checking correction about position of cross center points*/
//    QPoint* points = _data.screenPoints;
//    QPoint transformed_points[_algorithm_points];
//    transformed_points[QWSPointerCalibrationData::TopLeft] =
//            qt_screen->mapFromDevice(points[QWSPointerCalibrationData::TopLeft], screenSize);
//    transformed_points[QWSPointerCalibrationData::TopRight] =
//            qt_screen->mapFromDevice(points[QWSPointerCalibrationData::TopRight], screenSize);
//    transformed_points[QWSPointerCalibrationData::BottomLeft] =
//            qt_screen->mapFromDevice(points[QWSPointerCalibrationData::BottomLeft], screenSize);
//    transformed_points[QWSPointerCalibrationData::BottomRight] =
//            qt_screen->mapFromDevice(points[QWSPointerCalibrationData::BottomRight], screenSize);
//    transformed_points[QWSPointerCalibrationData::Center] =
//            qt_screen->mapFromDevice(points[QWSPointerCalibrationData::Center], screenSize);

//    p.fillRect(transformed_points[QWSPointerCalibrationData::TopLeft].x() - 1,
//            transformed_points[QWSPointerCalibrationData::TopLeft].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[QWSPointerCalibrationData::TopRight].x() - 1,
//            transformed_points[QWSPointerCalibrationData::TopRight].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[QWSPointerCalibrationData::BottomLeft].x() - 1,
//            transformed_points[QWSPointerCalibrationData::BottomLeft].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[QWSPointerCalibrationData::BottomRight].x() - 1,
//            transformed_points[QWSPointerCalibrationData::BottomRight].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[QWSPointerCalibrationData::Center].x() - 1,
//            transformed_points[QWSPointerCalibrationData::Center].y() - 1,  2,  2,  Qt::red);

    /*9 points algorithm*/
    /*This is test code for checking correction about position of cross center points*/
//    QPoint* points = _screen_nine_matrix;
//    QPoint transformed_points[_algorithm_points];
//    transformed_points[SCREEN_CENTRE] =
//        qt_screen->mapFromDevice(points[SCREEN_CENTRE], screenSize);
//    transformed_points[SCREEN_TOP] =
//        qt_screen->mapFromDevice(points[SCREEN_TOP], screenSize);
//    transformed_points[SCREEN_TOP_LEFT] =
//        qt_screen->mapFromDevice(points[SCREEN_TOP_LEFT], screenSize);
//    transformed_points[SCREEN_LEFT] =
//        qt_screen->mapFromDevice(points[SCREEN_LEFT], screenSize);
//    transformed_points[SCREEN_BOTTOM_LEFT] =
//        qt_screen->mapFromDevice(points[SCREEN_BOTTOM_LEFT], screenSize);
//    transformed_points[SCREEN_BOTTOM] =
//        qt_screen->mapFromDevice(points[SCREEN_BOTTOM], screenSize);
//    transformed_points[SCREEN_BOTTOM_RIGHT] =
//        qt_screen->mapFromDevice(points[SCREEN_BOTTOM_RIGHT], screenSize);
//    transformed_points[SCREEN_RIGHT] =
//        qt_screen->mapFromDevice(points[SCREEN_RIGHT], screenSize);
//    transformed_points[SCREEN_TOP_RIGHT] =
//        qt_screen->mapFromDevice(points[SCREEN_TOP_RIGHT], screenSize);

//    p.fillRect(transformed_points[SCREEN_CENTRE].x() - 1, transformed_points[SCREEN_CENTRE].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_TOP].x() - 1, transformed_points[SCREEN_TOP].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_TOP_LEFT].x() - 1, transformed_points[SCREEN_TOP_LEFT].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_LEFT].x() - 1, transformed_points[SCREEN_LEFT].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_BOTTOM_LEFT].x() - 1, transformed_points[SCREEN_BOTTOM_LEFT].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_BOTTOM].x() - 1, transformed_points[SCREEN_BOTTOM].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_BOTTOM_RIGHT].x() - 1, transformed_points[SCREEN_BOTTOM_RIGHT].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_RIGHT].x() - 1, transformed_points[SCREEN_RIGHT].y() - 1,  2,  2,  Qt::red);
//    p.fillRect(transformed_points[SCREEN_TOP_RIGHT].x() - 1, transformed_points[SCREEN_TOP_RIGHT].y() - 1,  2,  2,  Qt::red);
}

void touch_calibration_nine::mousePressEvent(QMouseEvent* event)
{
    if(_pressCount >= _algorithm_points)
    {
        if(_calibration_ret)
        {
            //int width = qt_screen->deviceWidth();
            //int height = qt_screen->deviceHeight();

            QDesktopWidget* desktopWidget = QApplication::desktop();
            QRect screenRect = desktopWidget->screenGeometry();

            int width = screenRect.width();
            int height = screenRect.height();

            int dx = width / 2;
            int dy = height / 2;

            int ltx = dx - 140;
            int lty = dy - 45;
            int rbx = dx + 140;
            int rby = dy + 45;

            QRegion reg(QRect(ltx, lty, rbx - ltx, rby - lty));

            if(!reg.contains(event->pos()))
            {
                // Give up recent calibration instead of using former one.
                QWSServer::mouseHandler()->calibrate(&_former_data);
//                write_calibration_paras();
//                QWSServer::mouseHandler()->calibrate(NULL);
                sync();
            }
        }
        _pressCount = 0;
        accept();
    }
    else
    {
        _total_num = 0;
        _average_index = 0;

        //Map from device coordinates in case the screen is transformed
        QSize screenSize(qt_screen->width(), qt_screen->height());
        QPoint p = qt_screen->mapToDevice(event->pos(), screenSize);

        //We must record the press point as no any point reported from mouseMoveEvent
        _average_array[0] = p;

        _press_timer->start(1000);
    }
}

void touch_calibration_nine::mouseReleaseEvent(QMouseEvent*)
{
    if(_pressCount >= _algorithm_points)
    {
        // Assure that the last release happened in QWSCalibratedMouseHandler::sendFiltered.
        QWSServer::mouseHandler()->calibrate(&_data);
        sync();

        if(!linear_coefficient_verify())
        {
            // Give up recent calibration instead of using former one.
            QWSServer::mouseHandler()->calibrate(&_former_data);
//            write_calibration_paras();
//            QWSServer::mouseHandler()->calibrate(NULL);
            sync();
        }
    }

    _press_timer->stop();
}

void touch_calibration_nine::mouseMoveEvent(QMouseEvent* event)
{
    //Map from device coordinates in case the screen is transformed
    QSize screenSize(qt_screen->width(), qt_screen->height());
    QPoint p = qt_screen->mapToDevice(event->pos(), screenSize);

    if(_average_index >= MCGS_CALIBRATION_PROBE_SET_NINE)
    {
        _average_index = 0;
    }
    _average_array[_average_index++] = p;
    _total_num++;
}

void touch_calibration_nine::accept()
{
    QDialog::accept();
}

void touch_calibration_nine::probe_calibration()
{
    _press_timer->stop();

    if(!_draw_timer->isActive())
    {
        if(_total_num < MCGS_CALIBRATION_PROBE_SET_NINE)
        {
            if(_total_num >= 1)
            {
                if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
                {
                    switch(_pressCount)
                    {
                        case SCREEN_CENTRE:
                        {
                            _data.devPoints[QWSPointerCalibrationData::Center] = _average_array[_total_num - 1];
                            break;
                        }
                        case SCREEN_TOP:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            _data.screenPoints[QWSPointerCalibrationData::BottomLeft] = _average_array[_total_num - 1];
                            break;
                        }
                        case SCREEN_TOP_LEFT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::TopLeft] = _average_array[_total_num - 1];
                            break;
                        }
                        case SCREEN_LEFT:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            int x = _data.screenPoints[QWSPointerCalibrationData::BottomLeft].x();
                            int y = _data.screenPoints[QWSPointerCalibrationData::BottomLeft].y();
                            int nx = _average_array[_total_num - 1].x() << 16;
                            int ny = _average_array[_total_num - 1].y() << 16;
                            int X = x | nx;
                            int Y = y | ny;
                            _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setX(X);
                            _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setY(Y);
                            break;
                        }
                        case SCREEN_BOTTOM_LEFT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::BottomLeft] = _average_array[_total_num - 1];
                            break;
                        }
                        case SCREEN_BOTTOM:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            _data.screenPoints[QWSPointerCalibrationData::Center] = _average_array[_total_num - 1];
                            break;
                        }
                        case SCREEN_BOTTOM_RIGHT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::BottomRight] = _average_array[_total_num - 1];
                            break;
                        }
                        case SCREEN_RIGHT:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            int x = _data.screenPoints[QWSPointerCalibrationData::Center].x();
                            int y = _data.screenPoints[QWSPointerCalibrationData::Center].y();
                            int nx = _average_array[_total_num - 1].x() << 16;
                            int ny = _average_array[_total_num - 1].y() << 16;
                            int X = x | nx;
                            int Y = y | ny;
                            _data.screenPoints[QWSPointerCalibrationData::Center].setX(X);
                            _data.screenPoints[QWSPointerCalibrationData::Center].setY(Y);
                            break;
                        }
                        case SCREEN_TOP_RIGHT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::TopRight] = _average_array[_total_num - 1];
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
                else
                {
                    _data.devPoints[_pressCount] = _average_array[_total_num - 1];
                }
            }
            else if(_total_num == 0)
            {
                if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
                {
                    switch(_pressCount)
                    {
                        case SCREEN_CENTRE:
                        {
                            _data.devPoints[QWSPointerCalibrationData::Center] = _average_array[_total_num];
                            break;
                        }
                        case SCREEN_TOP:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            _data.screenPoints[QWSPointerCalibrationData::BottomLeft] = _average_array[_total_num];
                            break;
                        }
                        case SCREEN_TOP_LEFT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::TopLeft] = _average_array[_total_num];
                            break;
                        }
                        case SCREEN_LEFT:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            int x = _data.screenPoints[QWSPointerCalibrationData::BottomLeft].x();
                            int y = _data.screenPoints[QWSPointerCalibrationData::BottomLeft].y();
                            int nx = _average_array[_total_num - 1].x() << 16;
                            int ny = _average_array[_total_num - 1].y() << 16;
                            int X = x | nx;
                            int Y = y | ny;
                            _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setX(X);
                            _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setY(Y);
                            break;
                        }
                        case SCREEN_BOTTOM_LEFT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::BottomLeft] = _average_array[_total_num];
                            break;
                        }
                        case SCREEN_BOTTOM:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            _data.screenPoints[QWSPointerCalibrationData::Center] = _average_array[_total_num];
                            break;
                        }
                        case SCREEN_BOTTOM_RIGHT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::BottomRight] = _average_array[_total_num];
                            break;
                        }
                        case SCREEN_RIGHT:
                        {
                            //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                            //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                            int x = _data.screenPoints[QWSPointerCalibrationData::Center].x();
                            int y = _data.screenPoints[QWSPointerCalibrationData::Center].y();
                            int nx = _average_array[_total_num - 1].x() << 16;
                            int ny = _average_array[_total_num - 1].y() << 16;
                            int X = x | nx;
                            int Y = y | ny;
                            _data.screenPoints[QWSPointerCalibrationData::Center].setX(X);
                            _data.screenPoints[QWSPointerCalibrationData::Center].setY(Y);
                            break;
                        }
                        case SCREEN_TOP_RIGHT:
                        {
                            _data.devPoints[QWSPointerCalibrationData::TopRight] = _average_array[_total_num];
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
                else
                {
                    _data.devPoints[_pressCount] = _average_array[_total_num];
                }
            }
            else
            {}
        }
        else
        {
            QPoint sumP(0, 0);
            for(int i = 0; i < MCGS_CALIBRATION_PROBE_SET_NINE; i++)
            {
                sumP += _average_array[i];
            }

            if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
            {
                switch(_pressCount)
                {
                    case SCREEN_CENTRE:
                    {
                        _data.devPoints[QWSPointerCalibrationData::Center].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.devPoints[QWSPointerCalibrationData::Center].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    case SCREEN_TOP:
                    {
                        //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                        //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                        _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    case SCREEN_TOP_LEFT:
                    {
                        _data.devPoints[QWSPointerCalibrationData::TopLeft].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.devPoints[QWSPointerCalibrationData::TopLeft].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    case SCREEN_LEFT:
                    {
                        //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                        //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                        int x = _data.screenPoints[QWSPointerCalibrationData::BottomLeft].x();
                        int y = _data.screenPoints[QWSPointerCalibrationData::BottomLeft].y();
                        int nx = (sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE) << 16;
                        int ny = (sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE) << 16;
                        int X = x | nx;
                        int Y = y | ny;
                        _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setX(X);
                        _data.screenPoints[QWSPointerCalibrationData::BottomLeft].setY(Y);
                        break;
                    }
                    case SCREEN_BOTTOM_LEFT:
                    {
                        _data.devPoints[QWSPointerCalibrationData::BottomLeft].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.devPoints[QWSPointerCalibrationData::BottomLeft].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    case SCREEN_BOTTOM:
                    {
                        //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                        //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                        _data.screenPoints[QWSPointerCalibrationData::Center].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.screenPoints[QWSPointerCalibrationData::Center].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    case SCREEN_BOTTOM_RIGHT:
                    {
                        _data.devPoints[QWSPointerCalibrationData::BottomRight].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.devPoints[QWSPointerCalibrationData::BottomRight].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    case SCREEN_RIGHT:
                    {
                        //因兼容性考虑，须占用屏幕点数据位，并将32位整数存储为两个16位整数。
                        //由于有两个屏幕点（左下和中心）在老触摸校准算法中未使用，故可被占用。
                        int x = _data.screenPoints[QWSPointerCalibrationData::Center].x();
                        int y = _data.screenPoints[QWSPointerCalibrationData::Center].y();
                        int nx = (sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE) << 16;
                        int ny = (sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE) << 16;
                        int X = x | nx;
                        int Y = y | ny;
                        _data.screenPoints[QWSPointerCalibrationData::Center].setX(X);
                        _data.screenPoints[QWSPointerCalibrationData::Center].setY(Y);
                        break;
                    }
                    case SCREEN_TOP_RIGHT:
                    {
                        _data.devPoints[QWSPointerCalibrationData::TopRight].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        _data.devPoints[QWSPointerCalibrationData::TopRight].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            else
            {
                _data.devPoints[_pressCount].setX(sumP.x() / MCGS_CALIBRATION_PROBE_SET_NINE);
                _data.devPoints[_pressCount].setY(sumP.y() / MCGS_CALIBRATION_PROBE_SET_NINE);
            }
        }
        _total_num = 0;
        _average_index = 0;

        if(++_pressCount >= _algorithm_points)
        {
            //zhaolunhan 2019/6/3
            _step1->setText(QObject::trUtf8("新的校准设置已测定。"));
            //_step1->setText(L000000037);
            _step2->setText(QObject::trUtf8("点击按钮内区域接受新设置；点击按钮外区域保留原有设置。"));
           //_step2->setText(L000000038);

            repaint();
        }
        else
        {
            _which = -1;
            _draw_timer->start(MCGS_CALIBRATION_DRAWCROSS_TIMEOUT_NINE);
        }
    }
}

void touch_calibration_nine::draw_cross()
{
    if(++_which >= MCGS_CALIBRATION_SCREEN_POINTS_NINE - 1)
    {
        _draw_timer->stop();
    }

    //construct refresh filed without whole filed update
    QPoint point;
    QPoint last_point;

    if(qt_screen->deviceHeight() > MCGS_TPC_LARGE_DISPLAY_NINE)
    {
        point = _other_screen_points_for_nine[_pressCount - 1][_which];
        if(_which == 0)
        {
            last_point = _screen_nine_matrix[_pressCount - 1];
        }
        else
        {
            last_point = _other_screen_points_for_nine[_pressCount - 1][_which - 1];
        }
    }
    else
    {
        point = _other_screen_points[_pressCount - 1][_which];
        if(_which == 0)
        {
            last_point = _data.screenPoints[_pressCount - 1];
        }
        else
        {
            last_point = _other_screen_points[_pressCount - 1][_which - 1];
        }
    }


    //Map to logical coordinates in case the screen is transformed
    QSize screenSize(qt_screen->deviceWidth(), qt_screen->deviceHeight());
    point = qt_screen->mapFromDevice(point, screenSize);
    last_point = qt_screen->mapFromDevice(last_point, screenSize);

    QRect repaint_filed;
    if(qt_screen->deviceWidth() > 1024)
    {
        repaint_filed = QRect(point.x() - 30, point.y() - 30,  60, 60).united(
                              QRect(last_point.x() - 30, last_point.y() - 30,  60, 60));
    }
    else
    {
        repaint_filed = QRect(point.x() - 20, point.y() - 20,  40, 40).united(
                              QRect(last_point.x() - 20, last_point.y() - 20,  40, 40));
    }

    repaint(repaint_filed);
}

void touch_calibration_nine::forbid_window_popped()
{
    QList<QWSWindow*> wnd_list = QWSServer::instance()->clientWindows();

    for(int i = 0; i < wnd_list.size(); ++i)
    {
        QString name = wnd_list[i]->name();
        if(name.compare("mcgstoolwnd") == 0)
        {
            wnd_list[i]->hide();
        }
    }
}

void touch_calibration_nine::set_ui(QDialog* dlg)
{
    //Note: on X11 you also have to pass Qt::X11BypassWindowManagerHint for this flag to work correctly.
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    QGridLayout* grid_layout;
    QVBoxLayout* v_layout;
    QHBoxLayout* h_layout1;
    QHBoxLayout* h_layout2;

    QSpacerItem* h_spacer1;
    QSpacerItem* h_spacer2;
    QSpacerItem* h_spacer3;
    QSpacerItem* h_spacer4;

    QSpacerItem* v_spacer0;
    QSpacerItem* v_spacer1;
    QSpacerItem* v_spacer2;
    QSpacerItem* v_spacer3;

    grid_layout = new QGridLayout(dlg);
    grid_layout->setObjectName(QString::fromUtf8("grid_layout"));
    v_layout = new QVBoxLayout();
    v_layout->setObjectName(QString::fromUtf8("v_layout"));

    if(qt_screen->transformOrientation() == 1 ||
       qt_screen->transformOrientation() == 3)
    {
        v_spacer0 = new QSpacerItem(20, 120, QSizePolicy::Minimum, QSizePolicy::Preferred);
    }
    else
    {
        v_spacer0 = new QSpacerItem(20, 80, QSizePolicy::Minimum, QSizePolicy::Preferred);
    }

    v_layout->addItem(v_spacer0);

    h_layout1 = new QHBoxLayout();
    h_layout1->setObjectName(QString::fromUtf8("h_layout"));
    h_layout1->setContentsMargins(0, 20, 0, 0);
    h_spacer1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    h_layout1->addItem(h_spacer1);

    _step1 = new QLabel(dlg);
    _step1->setObjectName(QString::fromUtf8("_step1"));
    _step1->setAutoFillBackground(false);
    _step1->setOpenExternalLinks(false);

    h_layout1->addWidget(_step1);

    h_spacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    h_layout1->addItem(h_spacer2);

    v_layout->addLayout(h_layout1);

    h_layout2 = new QHBoxLayout();
    h_layout2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
    h_spacer3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    h_layout2->addItem(h_spacer3);

    _step2 = new QLabel(dlg);
    _step2->setObjectName(QString::fromUtf8("_step2"));

    h_layout2->addWidget(_step2);

    h_spacer4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    h_layout2->addItem(h_spacer4);

    v_layout->addLayout(h_layout2);

    v_spacer1 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    v_layout->addItem(v_spacer1);

    v_spacer2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    v_layout->addItem(v_spacer2);

    v_spacer3 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    v_layout->addItem(v_spacer3);

    grid_layout->addLayout(v_layout, 0, 0, 7, 7);

    //zhaolunhan 2019/6/3
    _step1->setText(QObject::trUtf8("将手指轻而准确地放置在十字光标的中心点，停留至少1秒时间。"));
    //_step1->setText(L000000039);
    _step2->setText(QObject::trUtf8("当目标在屏幕上移动时，重复该动作。"));
    //_step2->setText(L000000040);
}

void touch_calibration_nine::calibration_confirm_ui(QPainter* painter)
{
    //int width = qt_screen->deviceWidth();
    //int height = qt_screen->deviceHeight();

    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect screenRect = desktopWidget->screenGeometry();

    int width = screenRect.width();
    int height = screenRect.height();

    int dx = width / 2;
    int dy = height / 2;

    int ltx = dx - 140;
    int lty = dy - 45;
    int rtx = dx + 140;
    int rty = lty;
    int lbx = ltx;
    int lby = dy + 45;
    int rbx = rtx;
    int rby = lby;

    painter->fillRect(rect(), Qt::white);
    painter->fillRect(QRect(ltx, lty, rbx - ltx, rby - lty), Qt::lightGray);

    QPen pen;
    pen.setStyle(Qt::SolidLine);
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->drawLine(QPoint(ltx, lty), QPoint(rtx, rty));
    painter->drawLine(QPoint(ltx, lty), QPoint(lbx, lby));
    painter->drawLine(QPoint(rtx, rty), QPoint(rbx, rby));
    painter->drawLine(QPoint(lbx, lby), QPoint(rbx, rby));


    QFont font(painter->font());
    font.setPixelSize(painter->font().pixelSize() + 20);
    painter->setFont(font);

    font.setLetterSpacing(QFont::AbsoluteSpacing, 10);
    //zhaolunhan 2019/6/3
    painter->drawText(QRect(ltx, lty, rbx - ltx, rby - lty), Qt::AlignCenter, QObject::trUtf8("确认校准"));
    //painter->drawText(QRect(ltx, lty, rbx - ltx, rby - lty), Qt::AlignCenter, L000000041);
}

bool touch_calibration_nine::linear_coefficient_verify()
{
    QString calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE"));
    if(calFile.isEmpty())
    {
        calFile = QLatin1String("/etc/pointercal");
    }

    QFile file(calFile);
    if(file.open(QIODevice::ReadOnly))
    {
        int a, b, c, d, e, f, s;
        QTextStream t(&file);
        t >> a >> b >> c >> d >> e >> f >> s;
        //Note: s cannot be valued by 0 as transform need s to be divisor
        if(s == 0 || t.status() != QTextStream::Ok)
        {
            //zhaolunhan 2019/6/3
            qCritical("Corrupt calibration data");
            /*logger_running_critical_printf(FF::log::logger_module_gui, "GuiFrame",
                                           "Corrupt calibration data");
            _calibration_ret = false;*/
        }
    }
    else
    {
        //zhaolunhan 2019/6/3
        qDebug() << "Could not read calibration: " << calFile;
        /*logger_running_critical_printf(FF::log::logger_module_gui, "GuiFrame",
                                       "Could not read calibration: %s", calFile.toStdString().c_str());
        FF::log::monitor_report(FF::log::logger_module_gui, 10, "Could not read calibration!");*/
    }

    return _calibration_ret;
}

void touch_calibration_nine::read_calibration_paras()
{
    QString calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE"));
    if(calFile.isEmpty())
    {
        calFile = QLatin1String("/etc/pointercal");
    }

    QFile file(calFile);
    if(file.open(QIODevice::ReadOnly))
    {
        QTextStream t(&file);
        t >> _a >> _b >> _c >> _d >> _e >> _f >> _s;
        if(_s == 0 || t.status() != QTextStream::Ok)
        {
            //zhaolunhan 2019/6/3
            qCritical("Corrupt calibration data");
            /*logger_running_critical_printf(FF::log::logger_module_gui, "GuiFrame",
                                           "Corrupt calibration data");
            clear_calibration_paras();*/
        }
    }
    else
    {
        //zhaolunhan 2019/6/3
        qCritical("QWSCalibratedMouseHandler::readCalibration: "
                  "Could not read calibration from %s", qPrintable(calFile));
        /*logger_running_critical_printf(FF::log::logger_module_gui, "GuiFrame",
                                       "Could not read calibration from %s",
                                       calFile.toStdString().c_str());*/
    }
}

void touch_calibration_nine::write_calibration_paras()
{
    QString calFile;
    calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE"));
    if(calFile.isEmpty())
    {
        calFile = QLatin1String("/etc/pointercal");
    }

    QFile file(calFile);
    if(file.open(QIODevice::WriteOnly))
    {
        QTextStream t(&file);
        t << _a << ' ' << _b << ' ' << _c << ' ';
        t << _d << ' ' << _e << ' ' << _f << ' ' << _s << endl;
    }
    else
    {
        //zhaolunhan 2019/6/3
        qCritical("QWSCalibratedMouseHandler::writeCalibration: "
                  "Could not save calibration into %s", qPrintable(calFile));
        /*logger_running_critical_printf(FF::log::logger_module_gui, "GuiFrame",
                                       "Could not save calibration into %s",
                                       calFile.toStdString().c_str());*/
    }
}

void touch_calibration_nine::clear_calibration_paras()
{
    _a = 1;
    _b = 0;
    _c = 0;
    _d = 0;
    _e = 1;
    _f = 0;
    _s = 1;
}

void touch_calibration_nine::client_window_show()
{
    QList<QWSWindow*> wnd_list = QWSServer::instance()->clientWindows();

    for(int i = 0; i < wnd_list.size(); ++i)
    {
        QString name = wnd_list[i]->name();
        if(name.compare("mcgstoolwnd") == 0)
        {
            wnd_list[i]->show();
        }
    }
}
